package com.itheima.demo;

import com.itheima.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

public class Demo {
    public static void main(String[] args) throws Exception {

    test1();


    }
    //根据id查询
    public static void test2() throws Exception{
        //1.读取核心配置文件
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //2.构建SqlSessionFactory对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3.获取SqlSession对象
        SqlSession session = factory.openSession();
        //4.调用statement对象并执行
        User user = session.selectOne("one.findById", 1);
        System.out.println(user);
        //5.事务提交
        session.commit();
        //6.释放资源
        session.close();
        is.close();
    }

    //查询所有
    public static void test1() throws Exception{
    //1.读取核心配置文件
    InputStream is = Resources.getResourceAsStream("sql.xml");
    //2.构建SqlSessionFactory对象
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
    //3.获取SqlSession对象
    SqlSession session = factory.openSession();
    //4.调用statement对象并执行
    List <User> list = session.selectList("one.findAll");
        System.out.println(list);
    //5.事务提交
        session.commit();
    //6.释放资源
        session.close();
        is.close();
    }
}
